local liancheng = fk.CreateSkill{
  name = "plum__liancheng",
}

Fk:loadTranslationTable{
  ["plum__liancheng"] = "连城",
  [":plum__liancheng"] = "当一名角色杀死一个势力最后的角色后，你可以令其摸牌、或回复体力、或使用弃牌堆中的装备牌至对应上限。"
  .."<br/>背水：失去〖连城〗或废除装备区。",

  ["#plum__liancheng-invoke"] = "连城：是否令 %dest 执行一项？",
  ["plum__liancheng_draw"] = "摸牌至手牌上限",
  ["plum__liancheng_recover"] = "回复体力至体力上限",
  ["plum__liancheng_equip"] = "使用弃牌堆中的装备至装备区满",
  ["plum__liancheng_beishui1"] = "背水：你失去〖连城〗",
  ["plum__liancheng_beishui2"] = "背水：你废除所有装备栏",
}

liancheng:addEffect(fk.Deathed, {
  anim_type = "support",
  can_trigger = function (self, event, target, player, data)
    if player:hasSkill(liancheng.name)
    and table.every(player.room.alive_players, function (p)
      return p.kingdom ~= target.kingdom
    end) and data.damage then
      local to = data.damage.from
      if to and to:isAlive() then
        return (to:getHandcardNum() < to:getMaxCards() or to:isWounded()
        or table.find(player.room.discard_pile, function (id)
          local card = Fk:getCardById(id)
          return card.type == Card.TypeEquip and to:hasEmptyEquipSlot(card.sub_type)
        end))
      end
    end
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    local to = data.damage.from
    if not to then return end
    local all_choices = {
      "plum__liancheng_draw",
      "plum__liancheng_recover",
      "plum__liancheng_equip",
      "plum__liancheng_beishui1",
      "plum__liancheng_beishui2",
      "Cancel",
    }
    local choices = {}
    if to:getHandcardNum() < to:getMaxCards() then
      table.insert(choices, all_choices[1])
    end
    if to:isWounded() then
      table.insert(choices, all_choices[2])
    end
    if table.find(room.discard_pile, function (id)
      local card = Fk:getCardById(id)
      return card.type == Card.TypeEquip and to:hasEmptyEquipSlot(card.sub_type)
    end) then
      table.insert(choices, all_choices[3])
    end
    if #choices > 1 then
      table.insert(choices, all_choices[4])
      if #player:getAvailableEquipSlots() > 0 then
        table.insert(choices, all_choices[5])
      end
    end
    table.insert(choices, "Cancel")

    local choice = room:askToChoice(player, {
      choices = choices,
      all_choices = all_choices,
      cancelable = false,
      prompt = "#plum__liancheng-invoke::"..to.id,
      skill_name = liancheng.name
    })
    if choice ~= "Cancel" then
      event:setCostData(self, choice)
      return true
    end
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local to = data.damage.from
    if not to then return end
    local choice = event:getCostData(self)
    if choice == "plum__liancheng_draw" then
      to:drawCards(to:getMaxCards() - to:getHandcardNum(), liancheng.name)
    elseif choice == "plum__liancheng_recover" then
      room:recover{
        who = to,
        num = to.maxHp - to.hp,
        recoverBy = player,
        skillName = liancheng.name,
      }
    elseif choice == "plum__liancheng_equip" then
      for _, slot in ipairs(to:getAvailableEquipSlots()) do
        if to.dead then return end
        local type = Util.convertSubtypeAndEquipSlot(slot)
        if to:hasEmptyEquipSlot(type) then
          local cards = table.filter(room.discard_pile, function(id)
            local card = Fk:getCardById(id)
            return card.sub_type == type and not to:isProhibited(to, card)
          end)
          if #cards > 0 then
            local card = Fk:getCardById(table.random(cards))
            room:useCard({
              from = to,
              tos = {to},
              card = card,
            })
          end
        end
      end
    else
      if choice == "plum__liancheng_beishui1" then
        room:handleAddLoseSkills(player, "-plum__liancheng", nil, true, false)
      else
        room:abortPlayerArea(player, player:getAvailableEquipSlots())
      end
      if to.dead then return end
      if to:getHandcardNum() < to:getMaxCards() then
        to:drawCards(to:getMaxCards() - to:getHandcardNum(), liancheng.name)
        if to.dead then return end
      end
      if to:isWounded() then
        room:recover{
          who = to,
          num = to.maxHp - to.hp,
          recoverBy = player,
          skillName = liancheng.name,
        }
        if to.dead then return end
      end
      if table.find(room.discard_pile, function (id)
        local card = Fk:getCardById(id)
        return card.type == Card.TypeEquip and to:hasEmptyEquipSlot(card.sub_type)
      end) then
        for _, slot in ipairs(to:getAvailableEquipSlots()) do
          if to.dead then return end
          local type = Util.convertSubtypeAndEquipSlot(slot)
          if to:hasEmptyEquipSlot(type) then
            local cards = table.filter(room.discard_pile, function(id)
              local card = Fk:getCardById(id)
              return card.sub_type == type and not to:isProhibited(to, card)
            end)
            if #cards > 0 then
              local card = Fk:getCardById(table.random(cards))
              room:useCard({
                from = to,
                tos = {to},
                card = card,
              })
            end
          end
        end
      end
    end
  end
})

return liancheng